package com.chatviewer.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.chatviewer.blog.base.BaseResponse;
import com.chatviewer.blog.base.CloudResult;
import com.chatviewer.blog.base.Result;
import com.chatviewer.blog.base.ResultUtils;
import com.gearwenxin.client.ernie.ErnieBotTurboClient;
import com.gearwenxin.entity.chatmodel.ChatBaseRequest;
import com.gearwenxin.entity.response.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Ge Mingjia
 * @date 2023/7/29
 */
@RestController
@RequestMapping("/turbo")
public class ErnieBotTurboController {

    @Resource
    private ErnieBotTurboClient ernieBotTurboClient;

    //------------------默认参数------------------//

    // 单轮对话

    @PostMapping(value = "/chat")
    public CloudResult chatSingle(@RequestBody Map<String, Object> map) {
//        JSONObject obj = (JSONObject) JSONObject.parse(body);
//        String msg = obj.getString("msg");
        String message = (String) map.get("msg");
        ChatResponse chatResponse = ernieBotTurboClient.chatSingle(message).block();
        String responseResult = chatResponse.getResult();
        System.out.println(responseResult);
        return  CloudResult.createBySuccessMessage(responseResult);
    }

    // 连续对话
    @PostMapping("/chats")
    public Result<ChatResponse> chatCont(String msg, String msgUid) {
        ChatResponse response = ernieBotTurboClient.chatCont(msg, msgUid).block();
        return Result.success(response);
    }

    // 流式返回，单次对话
    @GetMapping(value = "/stream/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatSingleStream(@RequestParam String msg) {
        Flux<ChatResponse> chatResponse = ernieBotTurboClient.chatSingleOfStream(msg);
        return chatResponse.map(response -> "data: " + response.getResult() + "\n\n");
    }

    // 流式返回，连续对话
    @PostMapping(value = "/stream/chats", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatContStream(@RequestBody Map<String, Object> map) {
        String msg = (String) map.get("msg");
        String msgUid = (String) map.get("msgUid");
        Flux<ChatResponse> chatResponse = ernieBotTurboClient.chatContOfStream(msg, msgUid);

        //System.out.println(chatResponse);
       // return chatResponse.map(response -> "data: " + response.getResult() + "\n\n");
        return chatResponse.map(response -> "" + response.getResult() + "\n\n");
    }

    //------------------自定义参数------------------//

    // 模板对话
    @PostMapping("/param/chat")
    public Result<ChatResponse> pChatSingle(@RequestBody ChatBaseRequest chatBaseRequest) {
        ChatResponse chatResponse = ernieBotTurboClient.chatSingle(chatBaseRequest).block();
        return Result.success(chatResponse);
    }

    // 连续对话
    @PostMapping("/param/chats")
    public Result<ChatResponse> pChatCont(@RequestBody ChatBaseRequest chatBaseRequest) {
        ChatResponse response = ernieBotTurboClient.chatCont(chatBaseRequest, chatBaseRequest.getUserId()).block();
        return Result.success(response);
    }

}
